<?php
namespace Admin\Controller;

use Common\Controller\AdminbaseController;
use Think\Upload;
class LunboController extends AdminbaseController {
//上传图片配置 保存路径
	public function Img_upload($file){

		//配置
		$config = array(
			"exts" => 'jpg,gif,png,jpeg',
		);
		$upload=new Upload($config);

		$add = $upload->upload(array($file));

		$path=array();
		$http = sp_get_host();
		$path = '';
		foreach($add as $v){
			$path .= "/Uploads/".trim($v['savepath'],'.').$v['savename'].',';
		}
		$result = substr($path,0,-1);
		return $result;
	}

//轮播图列表
	function lunbotu_lst(){
		$p = I("p");
		$count =  M("lunbotu l")
			->join(" left join " .C("DB_PREFIX") . "account a on l.uid = a.id")
			->join(" left join " .C("DB_PREFIX") . "tonggao t on l.uid = t.id")
			->count();
		if($count==0){
			$p = 0;
		}else{
			if(empty($p)){
				$p = 1;
			}
		}
		$totalpage = ceil($count/10);
		$page = $this->page($count,10);
		$sd = ($p-1)*10;
		$data = M("lunbotu l")
			->join(" left join " .C("DB_PREFIX") . "account a on l.uid = a.id")
			->join(" left join " .C("DB_PREFIX") . "tonggao t on l.uid = t.id")
			->field("l.*,a.nickname,a.phone,t.contacts name,t.phone mobile")
			->limit($page->firstRow . ',' . $page->listRows)
			->order("id desc")
			->select();
		$this->assign("data",$data);
		$this->assign("page", $page->show('Admin'));
		$this->assign("p",$p);
		$this->assign("sd",$sd);
		$this->assign("totalpage",$totalpage);
		$this->display();
	}
//轮播图添加
	function lunbotu_add(){
		if($_POST){
			$lianjie = I("lianjie");
			$phone = I("phone");
			$img = $this->Img_upload($_FILES['img']);
			if($lianjie==1){
				$data = M("account")->where("phone = $phone")->field("id,phone")->find();
				if($data['id']){
					$dat['uid'] = $data['id'];
					$dat['img'] = $img;
					$dat['ctime'] = time();
					$dat['type'] = 1;
					M("lunbotu")->add($dat);
					$this->success("添加成功",U("Lunbo/lunbotu_lst"));
				}else{
					$this->error("该手机号下没有没有注册用户信息");die;
				}
			}elseif($lianjie==2){
				$data = M("tonggao")->where("phone = $phone")->field("id,phone")->find();
				if($data['id']){
					$dat['tonggao_id'] = $data['id'];
					$dat['img'] = $img;
					$dat['ctime'] = time();
					$dat['type'] = 2;
					M("lunbotu")->add($dat);
					$this->success("添加成功",U("Lunbo/lunbotu_lst"));
				}else{
					$this->error("该手机号下没有发布过通告信息");die;
				}
			}
		}else{
			$this->display();
		}
	}
//轮播图删除
	function lunbotu_del(){
		$id = I("id");
       $r = M("lunbotu")->where("id = $id")->delete();
		if($r){
			$this->success("删除成功",U("Lunbo/lunbotu_lst"));
		}else{
			$this->error("删除失败");
		}
	}












}